package com.dk.controller;

import com.dk.pojo.Goods;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Deacription TODO
 * @Author Lenovo
 * @Date 2020/12/4 17:21
 * @Version 1.0
 **/
@Controller
public class IndexController {

    @RequestMapping("index")
    public String index(Model model, HttpServletRequest request){
        request.getSession().setAttribute("order","订单");

        String userName = "张三";
        String str = "<font color='red'>哈哈哈</font>";
        List<String> phone = Arrays.asList("华为","小米","苹果");
        //后台转换日期
        String sdf = new SimpleDateFormat("yyyy-MM-dd").format(new Date());

        List<Integer> ids = Arrays.asList(1111,2222,3333,9999);

        Goods goods = new Goods(8888,"iPad");

        model.addAttribute("userName",userName);
        model.addAttribute("str",str);
        model.addAttribute("phone",phone);
        model.addAttribute("sdf",sdf);
        model.addAttribute("d",new Date());
        model.addAttribute("aid",9999);
        model.addAttribute("ids",ids);
        model.addAttribute("goods",goods);

        return "index";
    }

    @RequestMapping("findByName")
    @ResponseBody
    public String findByName(String userName){

        return userName;
    }

    @RequestMapping("findById/{aid}")
    @ResponseBody
    public Integer findById(@PathVariable("aid") Integer aid){

        return aid;
    }

    @RequestMapping("testDate")
    @ResponseBody
    public String testDate(@DateTimeFormat(pattern = "yyyy-MM-dd") Date day){
        System.out.println(day);
        return "ok";
    }
}

